﻿---@diagnostic disable: trailing-space
local extension = Package:new("moepack_heyday")
extension.extensionName = "moepack"

Fk:loadTranslationTable{
  ["moepack_heyday"] = "萌包-韶华",
}

local U = require "packages.utility.utility"

local MoeFunc = require "packages.moepack.moepack_function"



local emo__huahu = fk.CreateViewAsSkill{
  name = "emo__huahu",
  pattern = ".|.|.|.|.|trick",
  prompt = "#emo__huahu-prompt",
  interaction = function()
    local names = {}
    local mark = Self:getTableMark("@$emo__huahu_trick")
    for _, n in ipairs(mark) do
      local card = Fk:cloneCard(n)
      if ((Fk.currentResponsePattern == nil and Self:canUse(card)) or
        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card))) then
        table.insertIfNeed(names, n)
      end
    end
    if #mark > 0 then
      return UI.CardNameBox {choices = names , all_choices = mark}
    end
  end,
  card_filter = function(self, to_select, selected)
    return Fk:currentRoom():getCardArea(to_select) ~= Player.Equip and #selected == 0
  end,
  card_num = 1,
  before_use = function(self, player, use)
    local room = player.room
    local mark = player:getMark("@$emo__huahu_trick")
    table.removeOne(mark, use.card.name)
    room:setPlayerMark(player, "@$emo__huahu_trick", #mark > 0 and mark or 0)
    room:addPlayerMark(player, "@@emo__huahu-turn")
  end,
  view_as = function(self, cards)
    if not self.interaction.data or #cards ~= 1 then return nil end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcard(cards[1])
    card.skillName = self.name
    return card
  end,
  enabled_at_play = function(self, player)
    if player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 and not player:isKongcheng() then
      local mark = player:getTableMark("@$emo__huahu_trick")
      return #mark > 0
    end
  end,
  enabled_at_response = function(self, player)
    if player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 and not player:isKongcheng() and Fk.currentResponsePattern then
      local mark = player:getTableMark("@$emo__huahu_trick")
      for _, n in ipairs(mark) do
        local card = Fk:cloneCard(n)
        if Exppattern:Parse(Fk.currentResponsePattern):match(card) then
          return true
        end
      end
    end
  end,

  on_lose = function (self, player)
    player.room:setPlayerMark(player, "@$emo__huahu_trick", 0) 
  end,
}

local emo__huahu_delay = fk.CreateTriggerSkill{
  name = "#emo__huahu_delay",
  anim_type = "drawcard",
  events = { fk.AfterCardsMove },
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(emo__huahu) and player:getMark("@@emo__huahu-turn") > 0 and player:isKongcheng() then
      for _, move in ipairs(data) do
        if move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, emo__huahu.name)
  end,

  refresh_events = {fk.AfterCardsMove},
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(emo__huahu, true) then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            local card = Fk:getCardById(info.cardId)
            if card.type == Card.TypeTrick then
              return true
            end
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark("@$emo__huahu_trick")
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          local card = Fk:getCardById(info.cardId)
          if card.type == Card.TypeTrick then
            table.insertIfNeed(mark, card.name)
          end
        end
      end
    end
    room:setPlayerMark(player, "@$emo__huahu_trick", mark)
  end,
}
emo__huahu:addRelatedSkill(emo__huahu_delay)



local moe__xiaoqing = General(extension, "moe__xiaoqing", "moe", 3, 3, General.Female)

local emo__zaofa = fk.CreateTriggerSkill{
  name = "emo__zaofa",
  events = {fk.EventPhaseStart},
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player.phase == Player.Start and player == target and player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local record = player:getTableMark("emo__zaofa_number")
    local zaofaList = {
      "god_salvation", "fire__slash" , "analeptic" , "peach" ,
      "dismantlement","snatch","iron_chain","collateral",
      "fire_attack","duel","archery_attack","savage_assault","ex_nihilo"
    }
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".",
      skipDrop = true,
    }
    room:judge(judge)
    local number = judge.card.number
    local name = zaofaList[number]
    local judgeCardId = judge.card:getEffectiveId()
    if name and not player.dead then
      room:addTableMark(player, "emo__zaofa_number", number)
      U.askForUseVirtualCard(room, player, name, nil, self.name, "#emo__zaofa-use:::"..name, true, true, true, true)
      if table.contains(record, number) and room:getCardArea(judgeCardId) == Card.Processing then
        room:obtainCard(player, judgeCardId, true, fk.ReasonJustMove, player.id, self.name)
      end
    end
    room:cleanProcessingArea({judgeCardId}, self.name)
  end,
}
moe__xiaoqing:addSkill(emo__zaofa)


local fubing = fk.CreateTriggerSkill{
  name = "emo__fubing",
  anim_type = "support",
  events = {fk.FinishJudge},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and data.card.color == Card.Black
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, "#emo__fubing-give", self.name, true, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(room:getPlayerById(self.cost_data.tos[1]), "@emo__fubing")
  end,
}

local emo__fubing_delay = fk.CreateTriggerSkill{
  name = "#emo__fubing_delay",
  anim_type = "defensive",
  events = {fk.AskForCardUse, fk.AskForCardResponse, fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:getMark("@emo__fubing") > 0 then
      if event == fk.Damaged then
        return data.card and data.card.trueName == "slash"
      else
        return data.cardName == "jink" or (data.pattern and Exppattern:Parse(data.pattern):matchExp("jink|0|nosuit|none"))
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damaged then
      return true
    else
      local choice = room:askForChoice(player, {"emo__fubing_use", "emo__fubing_judge", "Cancel"}, self.name, "#emo__fubing-ask")
      if choice ~= "Cancel" then
        self.cost_data = choice
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damaged then
      local num = player:getMark("@emo__fubing")
      room:setPlayerMark(player, "@emo__fubing", 0)
      player:drawCards(num, fubing.name)
    else
      local choice = self.cost_data
      if choice == "emo__fubing_use" then
        room:removePlayerMark(player, "@emo__fubing")
      else
        local judgeData = { who = player, reason = fubing.name, pattern = ".|.|heart,diamond" }
        room:judge(judgeData)
        if judgeData.card.color ~= Card.Red then return false end
      end
      local jinkCard = Fk:cloneCard("jink")
      jinkCard.skillName = fubing.name
      if event == fk.AskForCardUse then
        data.result = {
          from = player.id,
          card = jinkCard,
        }
        if data.eventData then
          data.result.toCard = data.eventData.toCard
          data.result.responseToEvent = data.eventData.responseToEvent
        end
      else
        data.result = jinkCard
      end
      return true
    end
  end,
}
fubing:addRelatedSkill(emo__fubing_delay)

moe__xiaoqing:addSkill(fubing)

Fk:loadTranslationTable{
  ["moe__xiaoqing"] = "小青",

  ["emo__zaofa"] = "造法",
  [":emo__zaofa"] = "准备阶段，你可以进行一次判定，然后你可以视为使用一张判定结果点数对应的牌名，若此前已判定过此点数，你获得判定牌。<br><b>（点数1-13对应：桃园，火杀，酒，桃，拆，顺，铁索，借刀，火攻，决斗，万箭，南蛮，无中）</b>",
  ["#emo__zaofa-use"] = "造法：你可以视为使用一张【%arg】",

  ["emo__fubing"] = "符兵",
  [":emo__fubing"] = "每当你的黑色判定牌生效后，你可以令一名角色获得1枚“符”标记：每当该角色需要使用或打出【闪】时，其可以移去1枚“符”视为使用之，或进行判定，若为红色则视为使用或打出之。每当其受到【杀】的伤害后，其移去所有“符”并摸等量张牌。",
  ["@emo__fubing"] = "符",
  ["#emo__fubing-give"] = "符兵：可以令一名角色获得“符”标记",
  ["#emo__fubing-ask"] = "符兵：你可以移去1枚“符兵”标记视为使用【闪】或进行判定，若为红色视为用【闪】",
  ["emo__fubing_use"] = "消耗标记",
  ["emo__fubing_judge"] = "进行判定",
  ["#emo__fubing_delay"] = "符兵",
}



local emo__feihua = fk.CreateTriggerSkill{
  name = "emo__feihua",
  anim_type = "control",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and not target.dead then
      local events = player.room.logic:getEventsOfScope(GameEvent.Turn, 1, function(e)
        return e.data[1] == target
      end, Player.HistoryGame)
      return #events > 0 and events[1] == player.room.logic:getCurrentEvent()
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local choices = {"emo__feihua_choice1", "emo__feihua_choice2", "Cancel"}
    local choice = room:askForChoice(player, choices, self.name, "#emo__feihua-choice:"..target.id)
    if choice ~= "Cancel" then
      self.cost_data = choice
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data:endsWith("1") then
      room:setPlayerMark(target, "@emo__feihua", math.random(13))
    else
      room:addPlayerMark(player, "emo__feihua_count")
      local n = player:getMark("emo__feihua_count")
      room:askForDiscard(target, n, n, true, self.name, false)
    end
  end,
}

local emo__feihua_delay = fk.CreateTriggerSkill{
  name = "#emo__feihua_delay",
  mute = true,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, _, player, data)
    if not player.dead and player:getMark("@emo__feihua") ~= 0 then
      for _, move in ipairs(data) do
        if move.to == player.id and (move.toArea >= 1 and move.toArea <= 3) then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).number == player:getMark("@emo__feihua") then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, _, player, data)
    local room = player.room
    room:setPlayerMark(player, "@emo__feihua", 0)
    local me = table.find(room.alive_players, function (p) return p:hasSkill(emo__feihua, true) end)
    if me then
      room:notifySkillInvoked(me, "emo__feihua", "support")
    end
    room:changeMaxHp(player, 1)
    if player.dead then return end
    room:recover { num = 2, skillName = self.name, who = player, recoverBy = player }
    if player.dead then return end
    player:drawCards(3, self.name)
  end
}
emo__feihua:addRelatedSkill(emo__feihua_delay)
moe__xiaolv:addSkill(emo__feihua)

Fk:loadTranslationTable{
  ["moe__xiaolv"] = "小绿",

  ["emo__feihua"] = "飞花",
  [":emo__feihua"] = "一名角色的首个回合结束时，你可以选一项：1.随机选择一个点数，当此点数的牌下一次进入该角色的区域内时，其增加一点体力上限、回复2点体力并摸三张牌；2.令其弃置X张牌（X为你选择2的次数）。",
  ["#emo__feihua_delay"] = "飞花",
  ["@emo__feihua"] = "飞花",
  ["emo__feihua_choice1"] = "随机选择一个点数，此点数的牌进入其的区域内时，其增加一点体力上限、回复2点体力并摸三张牌",
  ["emo__feihua_choice2"] = "令其弃牌",
  ["#emo__feihua-choice"] = "飞花：为 %src 选择一项",

  ["$emo__feihua1"] = "飞花如雨乱纷纷，风吹舞枝落满尘",
}

local moe__tongweiwei = General(extension, "moe__tongweiwei", "moe", 3, 3, General.Female)

local emo__suimeng = fk.CreateTriggerSkill{
  name = "emo__suimeng",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if target == player then
        return player:getMark("@emo__suimeng") > 0
      else
        return player:getMark("@emo__suimeng") < #player.room.alive_players
      end
    end 
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if target == player then
      room:removePlayerMark(player, "@emo__suimeng")
      local get = table.find(room.draw_pile, function (id)
        local card = Fk:getCardById(id)
        return (player:canUse(card) and not player:prohibitUse(card)) == (player.phase ~= Player.NotActive)
      end)
      if get then
        room:obtainCard(player, get, true, fk.ReasonJustMove, player.id, self.name)
      end
    else
      room:addPlayerMark(player, "@emo__suimeng")
    end
  end,

  refresh_events = {fk.Deathed},
  can_refresh = function(self, event, target, player, data)
    return player:getMark("@emo__suimeng") > #player.room.alive_players
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@emo__suimeng", #player.room.alive_players)
  end,

  on_lose = function (self, player)
    player.room:setPlayerMark(player, "@emo__suimeng", 0)
  end,
}
moe__tongweiwei:addSkill(emo__suimeng)

local emo__huanzhou = fk.CreateTriggerSkill{
  name = "emo__huanzhou",
  anim_type = "defensive",
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player and player:getMark("@emo__suimeng") > 0 then
      return data.damage > 1 or #player.room.logic:getActualDamageEvents(1, function(e) return e.data[1].to == player end) > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:removePlayerMark(player, "@emo__suimeng")
    data.damage = data.damage - 1
  end,
}
moe__tongweiwei:addSkill(emo__huanzhou)


Fk:loadTranslationTable{
  ["moe__tongweiwei"] = "童薇薇",
  ["#moe__tongweiwei"] = "一梦千秋",

  ["emo__suimeng"] = "碎梦",
  [":emo__suimeng"] = "锁定技，其他角色使用牌后，你获得1枚“碎梦”标记（标记数至多为存活角色数）。每当使你用一张牌后，你弃1枚“碎梦”标记，若是你回合内/回合外，你摸一张可以/无法在出牌阶段空闲时使用的牌。",
  ["@emo__suimeng"] = "碎梦",

  ["emo__huanzhou"] = "幻昼",
  [":emo__huanzhou"] = "当你受到伤害时，若本回合你已受到过伤害，或此伤害大于1点，你可以弃1枚“碎梦”标记，令此伤害-1。",
}

local moe__muyuan = General(extension, "moe__muyuan", "moe", 3, 3, General.Female)

local emo__damen = fk.CreateTriggerSkill{
  name = "emo__damen",
  anim_type = "control",
  frequency = Skill.Compulsory,
  events = {fk.CardUsing, fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardUsing then
      if player:hasSkill(self) and target ~= player and data.card.trueName == "slash" then
        return player:getMark("emo__damen-turn") == 0
      end
    else
      return player:hasSkill(self) and target == player and data.card.trueName == "slash"
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing then
      room:setPlayerMark(player, "emo__damen-turn", 1)
      room:doIndicate(player.id, {target.id})
      data.from = player.id
      if table.contains(TargetGroup:getRealTargets(data.tos), player.id) then
        -- 无视禁止技添加目标
        table.insert(data.tos, {target.id})
        room:sendLog{
          type = "#AddTargetsBySkill",
          from = player.id,
          to = {target.id},
          arg = self.name,
          arg2 = data.card:toLogString()
        }
      end
    else
      local n = 0
      if data.damageDealt then
        for _, v in pairs(data.damageDealt) do
          n = n + v
        end
      end
      if n > 0 then
        player:drawCards(n, self.name)
      else
        room:askForDiscard(player, 1, 1, true, self.name, false)
      end
    end
  end,
}
moe__muyuan:addSkill(emo__damen)

local emo__xianxiu = fk.CreateTriggerSkill{
  name = "emo__xianxiu",
  events = { fk.GameStart },
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) 
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, "#emo__xianxiu", self.name, true)
    if #tos > 0 then
      self.cost_data = tos[1]
      return true
    end
  end,  
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    room:changeMaxHp(to, 2)
    if to.dead then return end
    room:recover { num = math.min(2, to.maxHp-to.hp), skillName = self.name, who = to , recoverBy = player }
    if to.dead then return end
    to:drawCards(2, self.name)
  end,
}

moe__muyuan:addSkill(emo__xianxiu)

Fk:loadTranslationTable{
  ["moe__muyuan"] = "沐媛",
  ["#moe__muyuan"] = "大家闺秀",

  ["emo__damen"] = "达门",
  [":emo__damen"] = "锁定技，①每回合一次，每当一名其他角色使用【杀】时，将此【杀】使用者改为你，若此【杀】目标含有你，你令该角色也成为目标。②每当你使用的【杀】结算完毕后，此【杀】每造成一点伤害你摸一张牌，若未造成伤害，你须弃置一张牌。",

  ["emo__xianxiu"] = "显秀",
  [":emo__xianxiu"] = "游戏开始时，你可以令一名角色增加2点体力上限，回复2点体力，摸两张牌。",
  ["#emo__xianxiu"] = "显秀：可以令一名角色增加2点体力上限，回复2点体力，摸两张牌",
}

local moe__xiazhi = General(extension, "moe__xiazhi", "moe", 5, 5, General.Female)

local emo__mulie = fk.CreateActiveSkill{
  name = "emo__mulie",
  anim_type = "offensive",
  card_num = 0,
  target_num = 0,
  prompt = "#emo__mulie",
  interaction = function(self)
    local all_choices = {"emo__mulie1", "emo__mulie2", "emo__mulie3"}
    local choices = {}
    if #Self:getCardIds("e") > 0 then table.insert(choices, "emo__mulie1") end
    if #Self:getCardIds("h") > 0 then table.insert(choices, "emo__mulie2") end
    if Self:getHandcardNum() ~= 2 then table.insert(choices, "emo__mulie3") end
    if #choices > 0 then
      return UI.ComboBox { choices = choices, all_choices = all_choices }
    end
  end,
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    local suits = {}
    for _, id in pairs(player.player_cards[Player.Hand]) do
      table.insertIfNeed(suits, Fk:getCardById(id).suit)
    end
    if #suits == #player:getCardIds("e") then
      return not player:isNude() or player:getHandcardNum() ~= 2
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local choice = self.interaction.data
    if choice == nil then return end
    if choice == "emo__mulie1" then
      room:recastCard(player:getCardIds("e"), player, self.name)
      if not player.dead then
        U.askForUseVirtualCard(room, player, "duel", nil, self.name, nil, false)
      end
    elseif choice == "emo__mulie2" then
      room:recastCard(player:getCardIds("h"), player, self.name)
      if not player.dead then
        U.askForUseVirtualCard(room, player, "fire_attack", nil, self.name, nil, false)
      end
    else
      local x = player:getHandcardNum() - 2
      if x > 0 then
        room:askForDiscard(player, x, x, false, self.name, false)
      else
        player:drawCards(-x, self.name)
      end
    end
  end,
}

moe__xiazhi:addSkill(emo__mulie)

Fk:loadTranslationTable{
  ["moe__xiazhi"] = "夏芷",

  ["emo__mulie"] = "穆烈",
  [":emo__mulie"] = "出牌阶段，若你装备区牌数等于手牌花色数，你可以选一项：1.重铸装备区牌，视为使用【决斗】；2.重铸手牌，视为使用【火攻】；3.将手牌数调整至两张。",
  ["emo__mulie1"] = "重铸装备，使用【决斗】",
  ["emo__mulie2"] = "重铸手牌，使用【火攻】",
  ["emo__mulie3"] = "将手牌数调整至两张",
  ["#emo__mulie"] = "穆烈：你装备区牌数等于手牌花色数，可以选一项",
}


local moe__baikeke = General(extension, "moe__baikeke", "moe", 4, 4, General.Female)

-- 注：请勿在洗牌时执行任何实际操作，避免诡异插结。应延迟到其他时机发动
local emo__xincheng = fk.CreateTriggerSkill{
  name = "emo__xincheng",
  events = {fk.RoundEnd},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player:getMark("emo__xincheng-round") > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player, 1)
    if not player.dead and player:isWounded() then
      room:recover { num = 1, skillName = self.name, who = player, recoverBy = player }
    end
    if not player.dead then
      player:gainAnExtraTurn(false, self.name)
    end
  end,

  refresh_events = {fk.AfterDrawPileShuffle},
  can_refresh = function(self, event, target, player, data)
    return player:getMark("emo__xincheng-round") == 0 
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "emo__xincheng-round", 1)
  end,
}

moe__baikeke:addSkill(emo__xincheng)

Fk:loadTranslationTable{
  ["emo__xincheng"] = "新程",
  [":emo__xincheng"] = "锁定技，每轮结束时，若本轮牌堆进行了洗牌，你增加一点体力上限并回复一点体力，并获得一个额外回合。",
}



local emo__yindie = fk.CreateActiveSkill{
  name = "emo__yindie",
  anim_type = "drawcard",
  can_use = function(self, player) return true end,
  card_num = 0,
  card_filter = function() return false end,
  target_num = 1,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id 
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:addPlayerMark(player, "emo__yindie_maxcards", 4)
    room:addPlayerMark(target, "emo__yindie_maxcards", 4)
    room:damage { from = target, to = player, damage = 1, skillName = self.name }
  end,
}

local emo__yindie_maxcards = fk.CreateMaxCardsSkill{
  name = "#emo__yindie_maxcards",
  correct_func = function(self, player)
    return player:getMark("emo__yindie_maxcards")
  end,
}
emo__yindie:addRelatedSkill(emo__yindie_maxcards)

local emo__yindie_ts = fk.CreateTriggerSkill{
  name = "#emo__yindie_ts",
  refresh_events = {fk.AfterTurnEnd},
  can_refresh = function(self, event, target, player, data)
    return target:getMark("emo__yindie_maxcards") > 0 and target == player
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(target, "emo__yindie_maxcards", 0)
    player.room:broadcastProperty(player, "MaxCards")
  end,
}
emo__yindie:addRelatedSkill(emo__yindie_ts)

moe__ruanruan:addSkill(emo__yindie)

local emo__wangqing = fk.CreateTriggerSkill{
  name = "emo__wangqing",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and data.from and not data.from.dead and data.from ~= player
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#emo__wangqing-ask:"..data.from.id)
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(2, self.name)
    if data.from.dead then return end
    data.from:drawCards(2, self.name)
    local room = player.room
    if data.from.phase == Player.Play and player:getHandcardNum() > 3 then
      local _event = player.room.logic:getActualDamageEvents(1, function(e) return e.data[1].from == data.from end, Player.HistoryPhase)
      local e = room.logic:getCurrentEvent():findParent(GameEvent.Damage)
      if e and #_event == 1 and _event[1].id == e.id then
        if #room:askForDiscard(player, 4, 4, false, self.name, true, ".", "#emo__wangqing-discard:"..data.from.id) == 4 then
          data.from:endPlayPhase()
        end
      end
    end
  end,
}
moe__ruanruan:addSkill(emo__wangqing)


local emo__huanen = fk.CreateTriggerSkill{
  name = "emo__huanen",
  anim_type = "support",
  events = {fk.Death},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self, false, true)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data.damage then
      data.damage.from = nil
    end
    if player:isNude() then return end
    local targets = {}
    local _event = room.logic:getEventsOfScope(GameEvent.Recover, 1, function(e)
      local dat = e.data[1]
      if dat.who == player then
        local who = dat.recoverBy
        if who and who ~= player and not who.dead then
          table.insertIfNeed(targets, who)
        end
      end
    end, Player.HistoryGame)
    if #targets > 0 then
      room:askForYiji(player, player:getCardIds("he"), targets, self.name, 0, 999, "#emo__huanen-give")
    end
  end,
}


Fk:loadTranslationTable{
  ["moe__ruanruan"] = "软软",

  ["emo__yindie"] = "引蝶",
  [":emo__yindie"] = "出牌阶段，你可以令一名其他角色对你造成1点伤害，然后你与其手牌上限+4直到各自回合结束。",

  ["emo__wangqing"] = "忘情",
  [":emo__wangqing"] = "每当你受到其他角色造成的伤害后，你可以与其各摸两张牌，若为其为出牌阶段，且为本阶段首次造成伤害，你可以弃置四张手牌令其结束出牌阶段。",
  ["#emo__wangqing-ask"] = "忘情：你可与 %src 各摸两张牌",
  ["#emo__wangqing-discard"] = "忘情：你可以弃置四张手牌令 %src 结束出牌阶段",

  ["emo__huanen"] = "还恩",
  [":emo__huanen"] = "锁定技，当你死亡时，视为没有伤害来源，然后你可以将你的牌分配给任意名本局游戏令你回复过体力的角色。",
  ["emo__huanen_active"] = "还恩",
  ["#emo__huanen-give"] = "还恩：可将你的牌分配给任意令你回复过体力的角色",
}






local emo__liwei = fk.CreateActiveSkill{
  name = "emo__liwei",
  anim_type = "control",
  prompt = "#emo__liwei",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isKongcheng()
    and table.find(Fk:currentRoom().alive_players, function(p) return player:canPindian(p) end)
  end,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 0,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local tos = table.filter(room:getAlivePlayers(), function (p)
      return player:canPindian(p)
    end)
    if #tos == 0 then return end
    local pindian = player:pindian(tos, self.name)
    if player.dead then return end
    local extra_data = {}
    extra_data[player.id] = pindian.fromCard.number
    for _, p in ipairs(tos) do
      local ret = pindian.results[p.id]
      if ret and ret.toCard then
        extra_data[p.id] = ret.toCard.number
      end
    end
    local card = Fk:cloneCard("pitch_camp")
    card.skillName = self.name
    if player:prohibitUse(card) then return end
    local useTars = table.filter(room:getAlivePlayers(), function (p)
      return not player:isProhibited(p, card)
    end)
    if #useTars == 0 then return end
    room:useCard{
      from = player.id,
      tos = table.map(useTars, function(p) return {p.id} end),
      card = card,
      extraUse = true,
      extra_data = {emo__liwei_info = extra_data}
    }
  end
}

local emo__liwei_trigger = fk.CreateTriggerSkill{
  name = "#emo__liwei_trigger",
  refresh_events = {fk.BeforeCardUseEffect},
  can_refresh = function(self, event, target, player, data)
    return player == target and data.extra_data and data.extra_data.emo__liwei_info
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local tos = TargetGroup:getRealTargets(data.tos)
    if #tos == 0 then return end
    local info = data.extra_data.emo__liwei_info
    local numbers, new_tos = {}, {}
    for pid, num in pairs(info) do
      table.insertIfNeed(numbers, num)  
    end
    -- 从大到小排列
    table.sort(numbers, function(a, b) return a > b end)
    for _, num in ipairs(numbers) do
      local playerFound = table.filter(tos, function(pid) return info[pid] == num end)
      table.insertTable(new_tos, playerFound)
      for _, pid in ipairs(playerFound) do
        table.removeOne(tos, pid)
      end
    end
    -- 未记录的剩余角色最末尾结算
    table.insertTable(new_tos, tos)
    room:sendLog{type = "#EmoLiweiLog", arg = data.card.name, to = new_tos, toast = true}
    data.tos = table.map(new_tos, function(p) return {p} end)
  end,
}
emo__liwei:addRelatedSkill(emo__liwei_trigger)

Fk:loadTranslationTable{
  ["emo__liwei"] = "立威",
  [":emo__liwei"] = "出牌阶段限一次，你可以与所有角色拼点，然后视为使用一张按拼点牌点数从高到低依次结算的【安营扎寨】。",
  ["#emo__liwei"] = "立威：与所有角色拼点，然后视为使用【安营扎寨】，点数约稿越优先结算",
  ["#emo__liwei_trigger"] = "立威",
  ["#EmoLiweiLog"] = "【%arg】结算顺序为 %to",
}

local moe__baizhu = General(extension, "moe__baizhu", "moe", 3, 3, General.Female)

local emo__tuixin = fk.CreateTriggerSkill{
  name = "emo__tuixin",
  anim_type = "control",
  events = {fk.Damaged, fk.EventPhaseStart},
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      return (event == fk.Damaged) or player.phase == Player.Start
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damaged then
      local tos = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, "#emo__tuixin-choose", self.name, true)
      if #tos > 0 then
        self.cost_data = tos[1]
        return true
      end
    else
      return room:askForSkillInvoke(player, self.name, nil, "#emo__tuixin-invoke")
    end
  end,  
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = (event == fk.Damaged) and room:getPlayerById(self.cost_data) or player
    local suits = {1,2,3,4}
    for _, id in ipairs(to:getCardIds("h")) do
      table.removeOne(suits, Fk:getCardById(id).suit)
    end
    local get = {}
    for _, id in ipairs(room.draw_pile) do
      if #suits == 0 then break end
      local suit = Fk:getCardById(id).suit
      if table.contains(suits, suit) then
        table.insert(get, id)
        table.removeOne(suits, suit)
      end
    end
    if #get > 0 then
      room:obtainCard(to, get, false, fk.ReasonPrey)
    end
    if to.dead then return end
    local cardsMap = {}
    for _, id in ipairs(to:getCardIds("h")) do
      local suit = Fk:getCardById(id).suit
      if suit ~= Card.NoSuit then
        cardsMap[suit] = cardsMap[suit] or {}
        table.insert(cardsMap[suit], id)
      end
    end
    local throw = {}
    for _, cards in pairs(cardsMap) do
      if #cards > 1 then
        table.remove(cards, math.random(#cards))
        table.insertTable(throw, cards)
      end
    end
    if #throw > 0 then
      room:throwCard(throw, "emo__tuixin", to, to)
    end
  end,
}
moe__baizhu:addSkill(emo__tuixin)

local emo__shiduan = fk.CreateActiveSkill{
  name = "emo__shiduan",
  anim_type = "support",
  card_num = 0,
  min_target_num = 1,
  card_filter = Util.FalseFunc,
  prompt = "#emo__shiduan-prompt",
  target_filter = function(self, to_select, selected)
    return Self.id ~= to_select
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:sortPlayersByAction(effect.tos)
    local success = 0
    for _, pid in ipairs(effect.tos) do
      local to = room:getPlayerById(pid)
      if not to.dead then
        local give = true
        if not to:isKongcheng() then
          if U.askForUseRealCard(room, to, nil, ".", self.name, "#emo__shiduan-use:"..player.id) then
            success = success + 1
            give = false
          end
        end
        if give and not player:isNude() then
          local card = room:askForCard(player, 1, 1, true, self.name, false, ".", "#emo__shiduan-give:"..to.id)
          room:obtainCard(to, card[1], false, fk.ReasonGive, player.id, self.name)
          success = success + 1
        end
      end
    end
    if success > player.hp and player:isWounded() and not player.dead then
      room:recover { num = 1, skillName = self.name, who = player, recoverBy = player }
    end
  end,
}

moe__baizhu:addSkill(emo__shiduan)

Fk:loadTranslationTable{
  ["moe__baizhu"] = "白烛",

  ["emo__tuixin"] = "推心",
  [":emo__tuixin"] = "①准备阶段，你可以获得手牌中没有的花色各一张，然后随机弃置手牌至每种花色各剩一张；②当你受到伤害后，你可以令一名角色执行〖推心〗①。",
  ["#emo__tuixin-choose"] = "推心：你可以令一名角色将手牌调整至每花色各剩一张",
  ["#emo__tuixin-invoke"] = "推心：你可以将手牌调整至每花色各剩一张",

  ["emo__shiduan"] = "识断",
  [":emo__shiduan"] = "出牌阶段限一次，你可以令任意名其他角色选一项：1.使用一张手牌；2.你交给其一张牌。若成功执行次数大于你体力值，你回复1点体力。",
  ["#emo__shiduan-use"] = "识断：请使用一张手牌，否则 %src 须交给你一张牌",
  ["#emo__shiduan-give"] = "识断：请交给 %src 一张牌",
  ["#emo__shiduan-prompt"] = "识断：你可令任意名其他角色使用一张牌，或你交给其一张牌",
}



local moe__jiangjiushi = General(extension, "moe__jiangjiushi", "moe", 4, 4, General.Female)

local emo__feijian = fk.CreateTriggerSkill{
  name = "emo__feijian",
  events = {fk.EventPhaseStart},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play and not player:isKongcheng()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(player:getCardIds("h")) do
      local c = Fk:getCardById(id,true)
      if c.suit == Card.Diamond then
        room:setCardMark(c, "@@emo__feijian-inhand", 1)
        Fk:filterCard(id, player)
      end
    end
  end,

  refresh_events = {fk.PreCardUse},
  can_refresh = function (self, event, target, player, data)
    return target == player and table.contains(data.card.skillNames, "emo__feijian")
  end,
  on_refresh = function (self, event, target, player, data)
    data.extraUse = true
  end,
}

local emo__feijian_delay = fk.CreateTriggerSkill{
  name = "#emo__feijian_delay",
  events = {fk.CardUseFinished},
  mute = true,
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and not player.dead and table.contains(data.card.skillNames, "emo__feijian") and not data.damageDealt
    and player.room:getCardArea(data.card) == Card.Processing
  end,
  on_use = function(self, event, target, player, data)
    player.room:obtainCard(player, data.card, true, fk.ReasonPrey)
  end,
}
emo__feijian:addRelatedSkill(emo__feijian_delay)

local emo__feijian_filter = fk.CreateFilterSkill{
  name = "#emo__feijian_filter",
  card_filter = function(self, card, player)
    return card:getMark("@@emo__feijian-inhand") > 0 and table.contains(player.player_cards[Player.Hand], card.id)
  end,
  view_as = function(self, card)
    local c = Fk:cloneCard("fire__slash", card.suit, card.number)
    c.skillName = "emo__feijian"
    return c
  end,
}
emo__feijian:addRelatedSkill(emo__feijian_filter)

local emo__feijian_targetmod = fk.CreateTargetModSkill{
  name = "#emo__feijian_targetmod",
  bypass_times = function(self, player, skill, scope, card)
    return card and table.contains(card.skillNames, "emo__feijian")
  end,
  bypass_distances = function(self, player, skill, card, to)
    return card and table.contains(card.skillNames, "emo__feijian")
  end,
}
emo__feijian:addRelatedSkill(emo__feijian_targetmod)

moe__jiangjiushi:addSkill(emo__feijian)

---@param player ServerPlayer
local getFuyanChoices = function (player, except)
  local choices = {}
  if player.chained or not player.faceup then table.insert(choices, "emo__fuyan_reset") end
  if #player:getCardIds("j") > 0 then table.insert(choices, "emo__fuyan_judge") end
  if player.shield ~= 1 then table.insert(choices, "emo__fuyan_sheild") end
  if player:getHandcardNum() < player.maxHp then table.insert(choices, "emo__fuyan_draw") end
  table.removeOne(choices, except)
  return choices
end

---@param player ServerPlayer
local doFuyan = function (player, choice)
  local room = player.room
  if choice == "emo__fuyan_reset" then
    player:reset()
  elseif choice == "emo__fuyan_judge" then
    local card = room:askForCardChosen(player, player, "j", "emo__fuyan")
    room:throwCard(card, "emo__fuyan", player, player)
  elseif choice == "emo__fuyan_sheild" then
    room:changeShield(player, 1 - player.shield)
  else
    player:drawCards(player.maxHp - player:getHandcardNum(), "emo__fuyan")
  end
end

local emo__fuyan = fk.CreateTriggerSkill{
  name = "emo__fuyan",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and player.phase == Player.Finish then
      return #player.room.logic:getEventsOfScope(GameEvent.UseCard, 999, function(e)
        local use = e.data[1]
        return use.from == player.id
      end, Player.HistoryTurn) >= player.hp
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player), function (p) return #getFuyanChoices(p) > 0 end)
    if #targets == 0 then return false end
    local tos = player.room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#emo__fuyan-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = tos[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    local choice = room:askForChoice(to, getFuyanChoices(to), self.name)
    doFuyan (to, choice)
    if not player.dead then
      local choices = getFuyanChoices(player, choice)
      if #choices > 0 then
        doFuyan (player, room:askForChoice(player, choices, self.name))
      end
    end
  end,
}
moe__jiangjiushi:addSkill(emo__fuyan)

Fk:loadTranslationTable{
  ["moe__jiangjiushi"] = "江酒诗",

  ["emo__feijian"] = "飞剑",
  [":emo__feijian"] = "锁定技，出牌阶段开始时，你手牌中的<font color='red'>♦</font>牌视为无距离和次数限制的火【杀】，此【杀】结算结束时，若未造成伤害，你获得此【杀】。",
  ["#emo__feijian_filter"] = "飞剑",
  ["@@emo__feijian-inhand"] = "飞剑",
  ["#emo__feijian_delay"] = "飞剑",

  ["emo__fuyan"] = "覆颜",
  [":emo__fuyan"] = "结束阶段，若你本回合使用的牌数不小于体力值，你可以令一名其他角色选一项，然后你选择另一项：1.重置武将牌；2.弃置判定区一张牌；3.护甲调整至一点；4.将手牌摸至体力上限。",
  ["#emo__fuyan-choose"] = "覆颜：你可以令一名其他角色选择执行“覆颜”的一项",
  ["emo__fuyan_reset"] = "重置武将牌",
  ["emo__fuyan_judge"] = "弃置判定区一张牌",
  ["emo__fuyan_sheild"] = "护甲调整至一点",
  ["emo__fuyan_draw"] = "手牌摸至体力上限",
}

local moe__falin = General(extension, "moe__falin", "moe", 4, 4, General.Female)

local emo__xinghai = fk.CreateTriggerSkill{
  name = "emo__xinghai",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and (data.extra_data or {}).emo__xinghaiCheck
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data.card.type == Card.TypeEquip then
      for _, id in ipairs(player.player_cards[Player.Hand]) do
        local c = Fk:getCardById(id)
        if c.type ~= Card.TypeEquip then
          room:setCardMark(c, "emo__xinghai-inhand", c:getMark("emo__xinghai-inhand") + (math.random(11) - 6))
          Fk:filterCard(id, player)
        end
      end
    else
      local pat = ".|.|.|.|.|equip,trick"
      if data.card.type == Card.TypeTrick then
        local n = data.card.number
        if n >= 13 then return end
        pat = ".|"..(n+1).."~13"
      end
      local ids = player.room:getCardsFromPileByRule(pat)
      if #ids > 0 then
        room:moveCardTo(ids, Card.PlayerHand, player, fk.ReasonPrey, self.name)
      end
    end
  end,

  refresh_events = {fk.PreCardUse},
  can_refresh = function (self, event, target, player, data)
    if player == target and not player:isKongcheng() and player:hasSkill(self) then
      local cardlist = Card:getIdList(data.card)
      if #cardlist == 1 and table.contains(player.player_cards[Player.Hand], cardlist[1]) then
        player:filterHandcards()
        return table.every(player.player_cards[Player.Hand], function(id)
          return Fk:getCardById(id).number >= Fk:getCardById(cardlist[1]).number
        end)
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.emo__xinghaiCheck = true
  end,
}
local emo__xinghai_filter = fk.CreateFilterSkill{
  name = "#emo__xinghai_filter",
  frequency = Skill.Compulsory,
  card_filter = function(self, to_select, player)
    return to_select:getMark("emo__xinghai-inhand") ~= 0 and table.contains(player.player_cards[Player.Hand], to_select.id)
  end,
  view_as = function(self, to_select, player)
    return Fk:cloneCard(to_select.name, to_select.suit, 
    math.max(1, math.min(13, to_select.number + to_select:getMark("emo__xinghai-inhand"))) )
  end,
}
emo__xinghai:addRelatedSkill(emo__xinghai_filter)

moe__falin:addSkill(emo__xinghai)


Fk:loadTranslationTable{
  ["moe__falin"] = "珐琳",

  ["emo__xinghai"] = "星海",
  [":emo__xinghai"] = "锁定技，每当你使用点数最小的一张手牌时，若此牌为：基本牌，摸一张非基本牌；锦囊牌，摸一张点数大于之的牌；装备牌，令手牌中的非装备牌点数随机变化（至少为1，至多为13）。",
  ["#emo__xinghai_filter"] = "星海",
}

local moe__yueyin = General(extension, "moe__yueyin", "moe", 3, 3, General.Female)

local emo__ganfa = fk.CreateActiveSkill{
  name = "emo__ganfa",
  anim_type = "offensive",
  card_num = 0,
  target_num = 2,
  card_filter = Util.FalseFunc,
  prompt = "#emo__ganfa-prompt",
  target_filter = function(self, to_select, selected)
    local slash = Fk:cloneCard("slash")
    slash.skillName = self.name
    local to = Fk:currentRoom():getPlayerById(to_select)
    if #selected < 2 and not to:prohibitUse(slash) then
      if #selected == 0 then return true end
      local first = Fk:currentRoom():getPlayerById(selected[1])
      return not first:isProhibited(to, slash) and not to:isProhibited(first, slash) and first.hp ~= to.hp
    end
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local tos = effect.tos
    room:sortPlayersByAction(tos)
    tos = table.map(tos, Util.Id2PlayerMapper)
    local damaged = {}
    for _, from in ipairs(tos) do
      local to = tos[1] == from and tos[2] or tos[1]
      if not from.dead and not to.dead then
        local slash = Fk:cloneCard("slash")
        slash.skillName = self.name
        local use = {from = from.id, tos = {{to.id}}, card = slash, extraUse = true}
        room:useCard(use)
        -- 考虑铁索传导、伤害转移
        if use.damageDealt then
          for _, p in ipairs({from, to}) do
            if use.damageDealt[p.id] then
              table.insertIfNeed(damaged, p)
            end
          end
        end
      end
    end
    for _, p in ipairs(tos) do
      if table.contains(damaged, p) then
        if not p.dead then
          local x = p:getHandcardNum() - p.hp
          if x > 0 then
            room:askForDiscard(p, x, x, false, self.name, false)
          end
        end
      else
        room:addTableMarkIfNeed(player, "emo__ganfa_role", p.id)
      end
    end
  end,
}
local emo__ganfa_visible = fk.CreateVisibilitySkill{
  name = "#emo__ganfa_visible",
  role_visible = function (self, player, target)
    if table.contains(player:getTableMark("emo__ganfa_role"), target.id) then
      return true
    end
  end,
}
emo__ganfa:addRelatedSkill(emo__ganfa_visible)
moe__yueyin:addSkill(emo__ganfa)

local emo__duanhong = fk.CreateTriggerSkill{
  name = "emo__duanhong",
  anim_type = "control",
  priority = 2, -- 为了防止后置位无法触发新获得的技能，需要提前
  events = {fk.Death}, 
  can_trigger = function(self, event, target, player, data)
    return (target == player and player:hasSkill(self, false, true)) or (target ~= player and player:hasSkill(self))
  end,
  on_cost = function(self, event, target, player, data)
    self.cost_data = {tos = {target.id}}
    return player.room:askForSkillInvoke(player, self.name, nil, "#emo__duanhong-invoke:"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skills = room:getTag("duanhong_skills")
    if skills == nil then
      local default_skills = {
        "duanchang", "huilei", "zhuiyi","zhongjiex","juexiang","os__yanhuo","ty__yanhuo",
        "qw__jincui","yechou","ol_ex__jieming", "fusong",
        "emo__yue", "emo__lihen",
      }
      skills = table.filter(default_skills, function (s)
        return Fk.skills[s] ~= nil
      end)
      -- 检索武将池的死亡技能
      for _, general_name in ipairs(room.general_pile) do
        local general = Fk.generals[general_name]
        for _, skill_name in ipairs(general:getSkillNameList()) do
          local skill = Fk.skills[skill_name]
          if skill:isInstanceOf(TriggerSkill) and type(skill.events) == "table" and #skill.events == 1 and skill.events[1] == fk.Death then
            table.insertIfNeed(skills, skill_name)
          end
        end
      end
      room:setTag("duanhong_skills", skills)
    end
    skills = table.filter(skills, function (s)
      return table.every(room.players, function(p) return not p:hasSkill(s, true, true) end)
    end)
    if #skills == 0 then return end
    local skill = room:askForChoice(player, table.random(skills, 3), self.name, nil, true)
    room:handleAddLoseSkills(target, skill)
  end,
}
moe__yueyin:addSkill(emo__duanhong)

Fk:loadTranslationTable{
  ["moe__yueyin"] = "月隐",

  ["emo__ganfa"] = "忓伐",
  [":emo__ganfa"] = "出牌阶段限一次，你可以选择体力值不相等的两名角色，令两者依次视为对对方使用一张【杀】。其中受到伤害的角色将手牌弃置至体力值，未受到伤害的你观看其身份牌。",
  ["#emo__ganfa-prompt"] = "忓伐：令两名体力值不等的角色互相视为使用【杀】！",

  ["emo__duanhong"] = "断鸿",
  [":emo__duanhong"] = "当一名角色死亡时，你可以从三个此时机的技能选择一个令其获得。",
  ["#emo__duanhong-invoke"] = "断鸿：你可以选择一个时机为“死亡时”的技能令 %src 获得",
}

local moe__liuxiaolu = General(extension, "moe__liuxiaolu", "moe", 3, 3, General.Female)

local emo__fengyuan = fk.CreateTriggerSkill{
  name = "emo__fengyuan",
  anim_type = "support",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target ~= player and target.phase == Player.Play and not target.dead then
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local name
    if target:getHandcardNum() > player:getHandcardNum() then
      name = "looting"
    elseif target:getHandcardNum() == player:getHandcardNum() then
      name = "sincere_treat"
    else
      name = "bogus_flower"
    end
    if not table.contains(player:getTableMark("emo__fengyuan-round"), name) and Fk.all_card_types[name] ~= nil then
      if room:askForSkillInvoke(player, self.name, nil, "#emo__fengyuan-ask:"..target.id.."::" ..name) then
        self.cost_data = {name = name, tos = {target.id}}
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local name = self.cost_data.name
    room:addTableMarkIfNeed(player, "emo__fengyuan-round", name)
    room:useVirtualCard(name, nil, player, target, self.name, true)
  end,
}
moe__liuxiaolu:addSkill(emo__fengyuan)

local fangfangTargetFilter = function (player, to)
  local mark, mark2 = player:getTableMark("emo__fangfang-turn"), player:getTableMark("emo__fangfang")
  return table.every(mark, function(v) return v[1] ~= to.id end) and table.every(mark2, function(v) return v[1] ~= to.id end)
end

local emo__fangfang = fk.CreateActiveSkill{
  name = "emo__fangfang",
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = "#emo__fangfang",
  card_filter = Util.FalseFunc,
  interaction = function ()
    return UI.ComboBox { choices = {"basic", "trick", "equip"} }
  end,
  target_filter = function(self, to_select, selected, cards, _, _, player)
    local to = Fk:currentRoom():getPlayerById(to_select)
    return #selected == 0 and player.id ~= to_select and fangfangTargetFilter(player, to)
  end,
  can_use = function(self, player)
    return table.find(Fk:currentRoom().alive_players, function (p)
      return self:targetFilter(p.id, {}, {}, nil, nil, player)
    end)
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    local choice = self.interaction.data
    room:addTableMark(player, "emo__fangfang-turn", {to.id, choice})
  end,
}

local emo__fangfang_delay = fk.CreateTriggerSkill{
  name = "#emo__fangfang_delay",
  mute = true,
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if not player.dead then
      return table.find(player:getTableMark("emo__fangfang"), function(v) return v[1] == target.id end) ~= nil
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local draw
    local mark = player:getTableMark("emo__fangfang")
    for i, v in ipairs(mark) do
      if v[1] == target.id then
        draw = (v[2] == data.card:getTypeString())
        table.remove(mark, i)
        break
      end
    end
    room:setPlayerMark(player, "emo__fangfang", mark)
    if draw then
      room:doIndicate(player.id, {target.id})
      room:notifySkillInvoked(player, "emo__fangfang", "drawcard")
      local ids = room:getCardsFromPileByRule(".|.|.|.|.|"..data.card:getTypeString())
      if #ids > 0 then
        room:obtainCard(player, ids, true, fk.ReasonJustMove, player.id, "emo__fangfang")
      end
    end
  end,

  refresh_events = {fk.TurnEnd},
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("emo__fangfang-turn") ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local mark, mark2 = player:getTableMark("emo__fangfang-turn"), player:getTableMark("emo__fangfang")
    table.insertTable(mark2, mark)
    room:setPlayerMark(player, "emo__fangfang", mark2)
  end,
}
emo__fangfang:addRelatedSkill(emo__fangfang_delay)
moe__liuxiaolu:addSkill(emo__fangfang)

Fk:loadTranslationTable{
  ["moe__liuxiaolu"] = "刘晓璐",

  ["emo__fengyuan"] = "逢源",
  [":emo__fengyuan"] = "每轮每项限一次，其他角色出牌阶段开始时，若其体力值大于/等于/小于你，你可以视为对其使用【趁火打劫】/【推心置腹】/【树上开花】。",
  ["#emo__fengyuan-ask"] = "逢源：你可以视为对 %src 使用一张【%arg】",

  ["emo__fangfang"] = "访芳",
  [":emo__fangfang"] = "出牌阶段，你可以选择一名其他角色，猜测自本回合结束起，其使用的第一张牌的类型，若你猜对，你获得一张同类型牌。",
  ["#emo__fangfang"] = "访芳：选择一种类型和一名角色，猜测其使用下一张牌的类型",
  ["#emo__fangfang_delay"] = "访芳",
}



local emo__jiaochou = fk.CreateTriggerSkill{
  name = "emo__jiaochou",
  events = { fk.GameStart},
  anim_type = "support",
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = table.filter(room.alive_players, function(p) return p.maxHp ~= player.maxHp end)
    if #tos == 0 then return end
    local to = table.random(tos)
    room:doIndicate(player.id, {to.id})
    room:delay(500)
    local list = {to.maxHp, player.maxHp}
    for i, p in ipairs({player, to}) do
      if not p.dead then
        local x = list[i] - p.maxHp
        room:changeMaxHp(p, x)
        if x > 0 then
          room:recover { num = x, skillName = self.name, who = p, recoverBy = player}
        elseif x < 0 then
          room:changeShield(p, -x)
          p:drawCards(-x ,self.name)
        end
      end
    end
  end,
}
moe__moqiubai:addSkill(emo__jiaochou)

Fk:loadTranslationTable{
  ["emo__jiaochou"] = "交筹",
  [":emo__jiaochou"] = "锁定技，游戏开始时，你随机与一名体力上限不等于你的角色交换体力上限，以此法扣减体力上限的角色获得等量护甲并摸等量张牌，以此法增加体力上限的角色回复等量体力。",
}

local moe__menglian = General(extension, "moe__menglian", "moe", 3, 3, General.Female)

local emo__sanhuo = fk.CreateTriggerSkill{
  name = "emo__sanhuo",
  events = {fk.Damaged},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and not target.dead and
    (data.damageType ~= fk.NormalDamage
    or (data.card and data.card.type == Card.TypeTrick)
    or table.every(player.room.alive_players, function(p) return p.hp >= target.hp end))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local numlist = function (p)
      local n1,n2,n3 = p:getMark("@emo__sanhuo1"),p:getMark("@emo__sanhuo2"),p:getMark("@emo__sanhuo3")
      return n1//3,n2//3,n3//3,(n1+n2+n3)//10
    end
    local n1,n2,n3,n4 = numlist(player)
    local num = data.damage > 1 and 2 or 1
    if data.card and data.card.type == Card.TypeTrick then
      room:addPlayerMark(player, "@emo__sanhuo1", num)
    end
    if data.damageType ~= fk.NormalDamage then
      room:addPlayerMark(player, "@emo__sanhuo2", num)
    end
    if table.every(player.room.alive_players, function(p) return p.hp >= target.hp end) then
      room:addPlayerMark(player, "@emo__sanhuo3", num)
    end
    local _n1,_n2,_n3,_n4 = numlist(player)
    local list = {}
    if _n1 > n1 then table.insert(list, "basic") end
    if _n2 > n2 then table.insert(list, "trick") end
    if _n3 > n3 then table.insert(list, "equip") end
    for _, s in ipairs(list) do
      if player.dead then break end
      local ids = room:getCardsFromPileByRule(".|.|.|.|.|"..s)
      if #ids > 0 then
        room:moveCardTo(ids, Card.PlayerHand, player, fk.ReasonPrey, self.name)
      end
    end
    if _n4 > n4 then
      if not player.dead and player:isWounded() then
        room:recover { num = 1, skillName = self.name, who = player, recoverBy = player }
      end
      local cards = table.filter(player:getCardIds("he"), function (id)
        return Fk:getCardById(id).is_damage_card or Fk:getCardById(id).sub_type == Card.SubtypeTreasure
      end)
      if #cards > 0 then
        room:recastCard(cards, player, self.name)
      end
    end
  end,
}
moe__menglian:addSkill(emo__sanhuo)

local emo__jinglian = fk.CreateActiveSkill{
  name = "emo__jinglian",
  frequency = Skill.Limited,
  prompt = "#emo__jinglian-prompt",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 1,
  target_filter = function (self, to_select, selected)
    return #selected == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    local x = math.min(5, room:getTag("RoundCount") or 1)
    room:addPlayerMark(to, "@emo__jinglian", x)
    room:recover { num = math.min(x, to.maxHp-to.hp), skillName = self.name, who = to, recoverBy = to }
  end,
}
local emo__jinglian_delay = fk.CreateTriggerSkill{
  name = "#emo__jinglian_delay",
  events = {fk.EventPhaseChanging, fk.DamageCaused, fk.DamageInflicted},
  frequency = Skill.Compulsory,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:getMark("@emo__jinglian") > 0 then
      if event == fk.EventPhaseChanging then
        return data.to == Player.Discard or data.to == Player.Judge
      else
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    MoeFunc.broadcastSkillDelay (player, "emo__jinglian", "defensive")
    if event == fk.EventPhaseChanging then
      player:skip(data.to)
    end
    return true
  end,

  refresh_events = {fk.TurnEnd},
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("@emo__jinglian") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:removePlayerMark(player, "@emo__jinglian", 1)
  end,
}
emo__jinglian:addRelatedSkill(emo__jinglian_delay)
moe__menglian:addSkill(emo__jinglian)

Fk:loadTranslationTable{
  ["moe__menglian"] = "梦莲",

  ["emo__sanhuo"] = "三火",
  [":emo__sanhuo"] = "锁定技，每当一名角色受到伤害后，若此伤害为锦囊伤害/属性伤害/受伤害角色体力值全场最小，你获得1个贪/嗔/痴标记，若伤害值大于1，额外获得1枚。每获得3枚贪/嗔/痴标记，你获得一张基本牌/锦囊牌/装备牌。每获得10枚标记，你回复一点体力并重铸你所有的伤害牌和武器牌。",
  ["@emo__sanhuo1"] = "贪",
  ["@emo__sanhuo2"] = "嗔",
  ["@emo__sanhuo3"] = "痴",

  ["emo__jinglian"] = "净莲",
  [":emo__jinglian"] = "限定技，出牌阶段，你可以令一名角色回复X点体力，其跳过判定和弃牌阶段，且防止其造成和受到伤害，直到其X个回合结束后（X为发动技能时的游戏轮数，至多为5）。",
  ["#emo__jinglian-prompt"] = "净莲：令一名角色回复体力，跳过判定和弃牌阶段，防止其造成和受到伤害",
  ["@emo__jinglian"] = "净莲",
  ["#emo__jinglian_delay"] = "净莲",

  ["$emo__jinglian1"] = "心静如水，脱尘遁世",
  ["$emo__jinglian2"] = "迷失者，回归本源吧",
}

local moe__ailan = General(extension, "moe__ailan", "moe", 3, 3, General.Female)

local emo__xisui = fk.CreateTriggerSkill{
  name = "emo__xisui",
  anim_type = "control",
  frequency = Skill.Compulsory,
  events = { fk.CardUseFinished },
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      local events = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        return e.data[1].from == player.id
      end, Player.HistoryTurn)
      return #events > 0 and events[1].data[1] == data
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data.card.suit == Card.NoSuit then
      player:drawCards(4, self.name)
    else
      local suit = data.card:getSuitString()
      local get = {}
      local ids1 = room:getCardsFromPileByRule(".|.|"..suit)
      if #ids1 > 0 then
        table.insert(get, ids1[1])
      end
      local ids2 = room:getCardsFromPileByRule(".|.|"..suit, 1, "discardPile")
      if #ids2 > 0 then
        table.insert(get, ids2[1])
      end
      local ids3, ids4 = {}, {}
      for _, p in ipairs(room:getOtherPlayers(player)) do
        for _, id in ipairs(p.player_cards[Player.Hand]) do
          if Fk:getCardById(id).suit == data.card.suit then
            table.insert(ids3, id)
          end
        end
        for _, id in ipairs(p.player_cards[Player.Equip]) do
          if Fk:getCardById(id).suit == data.card.suit then
            table.insert(ids4, id)
          end
        end
      end
      if #ids3 > 0 then
        table.insert(get, table.random(ids3))
      end
      if #ids4 > 0 then
        table.insert(get, table.random(ids4))
      end
      if #get > 0 then
        room:moveCardTo(get, Card.PlayerHand, player, fk.ReasonPrey, self.name)
      end
    end
    if not player.dead and data.card.type == Card.TypeBasic then
      room:loseHp(player, 1, self.name)
    end
  end,
}
moe__ailan:addSkill(emo__xisui)

local emo__mise = fk.CreateTriggerSkill{
  name = "emo__mise",
  events = {fk.HpRecover},
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = 0
    room.logic:getEventsOfScope(GameEvent.ChangeHp, 1, function(e)
      if e.data[1] == player and e.data[3] == "recover" then
        n = n + e.data[2]
      end
      return false
    end, Player.HistoryGame)
    n = math.min(n, #room.players)
    player:drawCards(n, self.name)
    if player.dead then return end
    if table.every(room.alive_players, function(p) return p.hp >= player.hp end) then
      room:changeShield(player, 1)
    end
    if table.every(room.alive_players, function(p) return p:getHandcardNum() <= player:getHandcardNum() end) then
      room:addPlayerMark(player, MarkEnum.AddMaxCards, 2)
    end
  end,
}
moe__ailan:addSkill(emo__mise)

Fk:loadTranslationTable{
  ["moe__ailan"] = "爱兰",
  ["#moe__ailan"] = "黑心护士",

  ["emo__xisui"] = "吸髓",
  [":emo__xisui"] = "锁定技，你每回合使用的第一张牌结算结束后，你从牌堆、弃牌堆、其他角色的手牌、装备区各随机获得一张同花色的牌（若使用的牌为无花色，改为摸四张牌），若你使用的牌为基本牌，你失去一点体力。",

  ["emo__mise"] = "蜜色",
  [":emo__mise"] = "当你回复体力后，你可以摸X张牌（X为你本局游戏回复体力总数，且至多为本局角色数），若你体力值最低，获得一点护甲，若你手牌数最多，手牌上限+2。",
}

local moe__bairuo = General(extension, "moe__bairuo", "moe", 4, 4, General.Female)

local emo__chuanyun = fk.CreateTriggerSkill{
  name = "emo__chuanyun",
  anim_type = "offensive",
  events = {fk.EventPhaseEnd, fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseEnd then
      return target.phase == Player.Play and player:hasSkill(self) and not player:isNude()
      and #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        return e.data[1].from == target.id
      end, Player.HistoryPhase) > 0
    else
      return target == player and player:hasSkill(self) and table.every(player.room:getOtherPlayers(player), function (p)
        return p:getAttackRange() < player:getAttackRange()
      end)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if event == fk.DamageCaused then return true end
    local types = {}
    room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
      local use = e.data[1]
      if use.from == target.id then
        table.insertIfNeed(types, use.card.type)
      end
    end, Player.HistoryPhase)
    local _, dat = room:askForUseViewAsSkill(player, "emo__chuanyun_active", "#emo__chuanyun-use", true, 
    {emo__chuanyun_types = types, bypass_times = true})
    if dat then
      self.cost_data = {types, dat}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseEnd then
      local types, dat = table.unpack(self.cost_data)
      while true do
        table.removeOne(types, Fk:getCardById(dat.cards[1]).type)
        local card = Fk:cloneCard("slash")
        card:addSubcard(dat.cards[1])
        card.skillName = self.name
        room:useCard{
          from = player.id,
          tos = table.map(dat.targets, function(p) return {p} end),
          card = card,
          extraUse = true,
        }
        if player.dead then break end
        room:addPlayerMark(player, "@emo__chuanyun")
        if player:isNude() or #types == 0 then break end
        local success, _dat = room:askForUseViewAsSkill(player, "emo__chuanyun_active", "#emo__chuanyun-use", true, 
        {emo__chuanyun_types = types, bypass_times = true })
        if success then
          dat = _dat
        else
          break
        end
      end
    else
      data.damage = data.damage + 1
      room:setPlayerMark(player, "@emo__chuanyun", 0)
    end
  end,
}
local emo__chuanyun_attackrange = fk.CreateAttackRangeSkill{
  name = "#emo__chuanyun_attackrange",
  correct_func = function (self, player)
    if player:hasSkill(emo__chuanyun) then
      return player:getMark("@emo__chuanyun")
    end
  end,
}
emo__chuanyun:addRelatedSkill(emo__chuanyun_attackrange)
moe__bairuo:addSkill(emo__chuanyun)

local emo__chuanyun_active = fk.CreateViewAsSkill{
  name = "emo__chuanyun_active",
  card_filter = function(self, to_select, selected)
    return #selected == 0 and table.contains(self.emo__chuanyun_types, Fk:getCardById(to_select).type)
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return nil end
    local c = Fk:cloneCard("slash")
    c.skillName = "emo__chuanyun"
    c:addSubcard(cards[1])
    return c
  end,
}
Fk:addSkill(emo__chuanyun_active)

local emo__shanque = fk.CreateTriggerSkill{
  name = "emo__shanque",
  events = {fk.AfterCardsMove},
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player ~= player.room.current and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then
      local area
      for _, move in ipairs(data) do
        if move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              if not area then
                area = info.fromArea
              else
                return false
              end
            end
          end
        end
      end
      if area then
        self.cost_data = area
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data == Card.PlayerEquip then
      player:drawCards(1, self.name)
    else
      local equip = table.find(room.draw_pile, function (id)
        return Fk:getCardById(id).type == Card.TypeEquip and player:hasEmptyEquipSlot(Fk:getCardById(id).sub_type)
      end)
      if equip then
        room:moveCardIntoEquip(player, equip, self.name, false, player)
      end
    end
  end,
}
moe__bairuo:addSkill(emo__shanque)


Fk:loadTranslationTable{
  ["moe__bairuo"] = "白若",

  ["emo__chuanyun"] = "穿云",
  [":emo__chuanyun"] = "①一名角色的出牌阶段结束时，该角色每使用一种类型的牌，你可以将一张同类型的牌当【杀】使用，然后你的攻击范围+1；②当你使用【杀】造成伤害时，若你的攻击范围唯一最高，令此伤害+1并失去以此法增加的攻击范围。",
  ["emo__chuanyun_active"] = "穿云",
  ["#emo__chuanyun-use"] = "穿云：将一张其使用过的类型的牌当【杀】使用",
  ["@emo__chuanyun"] = "穿云",

  ["emo__shanque"] = "山雀",
  [":emo__shanque"] = "锁定技，每回合一次，当你于回合外失去仅一张牌时，若失去的牌是手牌，随机将一张装备牌置于你的装备区；若失去的牌是装备区的牌，你摸一张牌。",
}

local moe__anran = General(extension, "moe__anran", "moe", 3, 3, General.Female)

local emo__dali = fk.CreateViewAsSkill{
  name = "emo__dali",
  pattern = ".|.|.|.|.|basic",
  interaction = function()
    local names = U.getViewAsCardNames(Self, "emo__dali", U.getAllCardNames("b"))
    if #names == 0 then return end
    return UI.ComboBox {choices = names}
  end,
  card_filter = function(self, to_select, selected)
    local card = Fk:getCardById(to_select)
    if not table.contains(Self.player_cards[Player.Hand], to_select) or card.suit == Card.NoSuit 
    or table.contains(Self:getTableMark("@emo__dali-round"), card:getSuitString(true)) then return false end
    return #selected == 0 or table.every(selected, function (id)
      return card.suit == Fk:getCardById(id).suit
    end)
  end,
  view_as = function(self, cards)
    if #cards == 0 or not self.interaction.data then return end
    if #cards ~= #table.filter(Self.player_cards[Player.Hand], function(id) return Fk:getCardById(cards[1]).suit == Fk:getCardById(id).suit end) then return nil end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)
    card.skillName = self.name
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    local mark = player:getTableMark("@emo__dali-round")
    table.insertIfNeed(mark, Fk:getCardById(use.card.subcards[1]):getSuitString(true))
    room:setPlayerMark(player, "@emo__dali-round", mark)
  end,
  after_use = function(self, player, use)
    local room = player.room
    if player.dead then return end
    local n = #use.card.subcards
    if n < 2 then return end
    local cards = room:getNCards(n-1)
    local suit = Fk:getCardById(use.card.subcards[1]).suit
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonPut, self.name, nil, true, player.id)
    local get = {}
    for _, id in ipairs(cards) do
      if Fk:getCardById(id).suit ~= suit then
        room:setCardEmotion(id, "judgegood")
        table.insert(get, id)
        room:delay(300)
      end
    end
    room:delay(800)
    if #get > 0 then
      room:moveCardTo(get, Card.PlayerHand, player, fk.ReasonPrey, self.name)
    end
    cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
    if #cards > 0 then
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, self.name)
    end
  end,
  enabled_at_play = function(self, player)
    return #player:getTableMark("@emo__dali-round") < 4 and not player:isKongcheng()
  end,
  enabled_at_response = function(self, player, response)
    if #player:getTableMark("@emo__dali-round") < 4 and not player:isKongcheng() and Fk.currentResponsePattern and not response then
      for _, name in ipairs(U.getAllCardNames("b")) do
        local card = Fk:cloneCard(name)
        if Exppattern:Parse(Fk.currentResponsePattern):match(card) then
          return true
        end
      end
    end
  end,
}
moe__anran:addSkill(emo__dali)

local emo__miaoyu = fk.CreateTriggerSkill{
  name = "emo__miaoyu",
  anim_type = "control",
  events = {fk.AfterCardTargetDeclared},
  can_trigger = function(self, event, target, player, data)
    return data.card:isCommonTrick() and player:hasSkill(self) and not player:isNude() 
    and player:usedSkillTimes(self.name, Player.HistoryTurn) < 2 and data.tos
    and (target == player or table.contains(TargetGroup:getRealTargets(data.tos), player.id))
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos, id = room:askForChooseCardAndPlayers(player, table.map(room:getOtherPlayers(player), Util.IdMapper), 1, 1, ".", "#emo__miaoyu-use", self.name, true)
    if #tos > 0 and id then
      self.cost_data = {tos[1], id}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data[1])
    local id = self.cost_data[2]
    room:moveCardTo(id, Card.PlayerHand, to, fk.ReasonGive, self.name, nil, false, player.id)
    local add = true
    if table.contains(TargetGroup:getRealTargets(data.tos), to.id) then
      player:drawCards(2, self.name)
      add = false
    end
    if target == to then
      add = false
      if not player.dead and not to:isNude() then
        local cards = room:askForCardsChosen(player, to, 0, 2, "he", self.name)
        if #cards > 0 then
          room:throwCard(cards, self.name, to, player)
        end
      end
    end
    if add and not to.dead and not target:isProhibited(to, data.card) and data.card.skill:getMinTargetNum() < 2 then
      table.insert(data.tos, {to.id})
    end
  end,
}
moe__anran:addSkill(emo__miaoyu)

Fk:loadTranslationTable{
  ["moe__anran"] = "安然",

  ["emo__dali"] = "达理",
  [":emo__dali"] = "每轮每花色限一次，你可以将一种花色的所有手牌当一种基本牌使用，此牌结算后亮出牌堆顶X-1张牌，获得其中不为此花色的牌（X为以此法转化的牌数）",
  ["@emo__dali-round"] = "达理",

  ["emo__miaoyu"] = "妙语",
  [":emo__miaoyu"] = "每回合限两次，当普通锦囊指定目标时，若你为使用者或目标之一，你可以交给一名其他角色一张牌，若该角色为：目标之一，你摸两张牌，使用者，你弃置其至多两张牌；不为两者，其成为额外目标。",
  ["#emo__miaoyu-use"] = "妙语：你可以交给一名其他角色一张牌",
}

local moe__longyue = General(extension, "moe__longyue", "moe", 3, 3, General.Female)

local emo__shenmi = fk.CreateActiveSkill{
  name = "emo__shenmi",
  anim_type = "drawcard",
  card_num = 0,
  min_target_num = 0,
  max_target_num = 2,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected, selected_cards, card, extra_data, player)
    return #selected < 2 and to_select ~= player.id
  end,
  prompt = "#emo__shenmi",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards = room:getNCards(3)
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonPut, self.name, nil, true)
    local players = {player}
    room:sortPlayersByAction(effect.tos)
    for _, pid in ipairs(effect.tos) do
      table.insert(players, room:getPlayerById(pid))
    end
    local flag = { card_data = { { self.name, cards } } }
    local req = Request:new(players, "AskForCardChosen")
    req.focus_text = self.name
    for _, p in ipairs(players) do
      local data = {player.id, flag, self.name, "#emo__shenmi-card"}
      req:setData(p, data)
      req:setDefaultReply(p, table.random(cards))
    end
    req:ask()
    local result = {}
    for _, p in ipairs(players) do
      result[p.id] = req:getResult(p)
    end
    room:delay(200)
    local myChoice = result[player.id]
    if player:isAlive() and room:getCardArea(myChoice) == Card.Processing then
      room:obtainCard(player, myChoice, true, fk.ReasonJustMove, player.id, self.name)
    end
    for _, p in ipairs(players) do
      if p ~= player and not p.dead and result[p.id] == myChoice then
        room:doIndicate(player.id, {p.id})
        room:damage { from = player, to = p, damage = 1, skillName = self.name }
      end
    end
    for _, p in ipairs(players) do
      local cid = result[p.id]
      if not p.dead and room:getCardArea(cid) == Card.Processing then
        local get = true
        for pid, ret in pairs(result) do
          if pid ~= p.id and ret == cid then
            get = false
          end
        end
        if get then
          room:obtainCard(p, cid, true, fk.ReasonJustMove, player.id, self.name)
        end
      end
    end
    room:cleanProcessingArea(cards, self.name)
  end,
}
moe__longyue:addSkill(emo__shenmi)

local emo__yaoyue = fk.CreateTriggerSkill{
  name = "emo__yaoyue",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.Damaged},
  on_use = function(self, event, target, player, data)
    local room = player.room
    local types = {"basic", "trick", "equip"}
    room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
      local use = e.data[1]
      table.removeOne(types, use.card:getTypeString())
      return #types == 0
    end, Player.HistoryTurn)
    if #types == 0 then 
      room:invalidateSkill(player, self.name, "-turn")
      return
    end
    local ids = {}
    for _, t in ipairs(types) do
      local cid = room:getCardsFromPileByRule(".|.|.|.|.|"..t)[1]
      if cid then
        table.insert(ids, cid)
      end
    end
    if #ids > 0 then
      room:moveCardTo(ids, Card.PlayerHand, player, fk.ReasonPrey, self.name)
    end
  end,
}
moe__longyue:addSkill(emo__yaoyue)

Fk:loadTranslationTable{
  ["moe__longyue"] = "胧月",

  ["emo__shenmi"] = "神迷",
  [":emo__shenmi"] = "出牌阶段限一次，你可以亮出牌堆顶三张牌，令至多两名其他角色与你同时选择其中一张牌，你获得你选择牌并对选择与你相同的其他角色造成1点伤害，其余牌若仅被一名角色选择，其获得之。",
  ["#emo__shenmi-card"] = "神迷：选择一张牌获得！",
  ["#emo__shenmi"] = "神迷：选择至多两名其他角色，亮出3张牌，同时选1张获得，选择与你相同的角色受伤害",
  
  ["emo__yaoyue"] = "摇月",
  [":emo__yaoyue"] = "锁定技，每当你受到伤害后，你获得本回合未被使用过的类型各一张牌。",
}











--[[

Fk:loadTranslationTable{
  [""] = "",

  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
}

--]]


return extension

